/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ejbs;

import dtos.FuncArmazemDTO;
import entities.Farmacia;
import entities.FuncArmazem;
import exceptions.EntidadeExistenteException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Yeezus
 */
@Stateless
public class FuncArmazemBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext(name = "Gestao_FarmaciasPU")
    private EntityManager em;

    @EJB
    private FuncionarioBean funcBean;

    public void criarFuncArmazem(String username, String password, String nome, String email, String farmacia) throws EntidadeExistenteException {
        try {
            if (funcBean.existeFuncionario(username)) {
                throw new EntidadeExistenteException("Esse funcionario já existe");
            }
            em.persist(new FuncArmazem(username, password, nome, email, em.find(Farmacia.class, farmacia)));

        } catch (EntidadeExistenteException e) {
            throw e;
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    public void atualizar(String username, String newUsername, String newPassword, String newName, String newEmail, String newFarmacia) {
        em.find(FuncArmazem.class, username).setEmail(newEmail);
        em.find(FuncArmazem.class, username).setPassword(newPassword);
        em.find(FuncArmazem.class, username).setNome(newName);
        em.find(FuncArmazem.class, username).setFarmacia(em.find(Farmacia.class, newFarmacia));
        em.find(FuncArmazem.class, username).setUsername(newUsername);
    }

    public FuncArmazemDTO getFuncArmazem(String username) {
        try {
            return copiarFuncArmazemParaDTO(em.find(FuncArmazem.class, username));
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    public List<FuncArmazemDTO> getAllFuncsArmazem() {
        try {
            List<FuncArmazem> funcs = em.createNamedQuery("findAllFuncsArmazem").getResultList();
            return copiarFuncsArmazemParaDTO(funcs);
        } catch (Exception e) {
            throw new EJBException(e.getMessage());
        }
    }

    private FuncArmazemDTO copiarFuncArmazemParaDTO(FuncArmazem func) {
        return new FuncArmazemDTO(func.getPassword(), func.getUsername(), func.getNome(), func.getEmail(), func.getFarmacia().getNome());
    }

    private List<FuncArmazemDTO> copiarFuncsArmazemParaDTO(List<FuncArmazem> funcs) {
        List<FuncArmazemDTO> dtos = new ArrayList<>();

        for (FuncArmazem func : funcs) {
            dtos.add(new FuncArmazemDTO(func.getPassword(), func.getUsername(), func.getNome(), func.getEmail(), func.getFarmacia().getNome()));
        }

        return dtos;
    }

}
